﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Enterprise.Service.Doc;
using Enterprise.Model.Doc;
using Enterprise.Infrastructure;
using Enterprise.Service.User;
using Enterprise.Model.Module;
using Enterprise.Service.Module;
using Enterprise.Control;
namespace Enterprise.UI.Web.Modules.Doc
{
    public partial class ArticlePub : Enterprise.Service.BasePage
    {
        public int Id = (int)Utility.sink("Id", Infrastructure.Utility.MethodType.Get, 0, 0, Infrastructure.Utility.DataType.Int);
        int ModuleId = (int)Utility.sink("ModuleId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int ChildId = (int)Utility.sink("ChildId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string mName = "";
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Cmd))
                {
                    bindArticleClass();
                    InitArticle();
                }
                else if (Cmd == "Delete")
                {
                    string _note = "操作成功";
                    try
                    {
                        DocService service = new DocService();
                        var q = service.DocArticleDisp(Id);
                        if (q != null)
                        {
                            q.DB_Option_Action = "Delete";
                            service.DocArticleExecute(q);
                        }
                    }
                    catch (Exception ex)
                    {
                        _note = ex.Message;
                    }
                    Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx?ModuleId=" + ModuleId + "&ChildId=" + ChildId);
                }
            }
        }

        private void InitArticle()
        {
            DocArticleEntity model = new DocArticleEntity();
            DocService service = new DocService();
            model = service.DocArticleDisp(Id);
            if (model != null)
            {
                //初始化信息到页面
                hid.Value = model.ID.ToString();
                tb_classid.SelectedValue = model.CLASSID.ToString();
                tb_content.Text = model.CONTENTS;
                tb_contentru.Text = model.CONTENTSRU;
                tb_from.Text = model.ARTICLEFROM;
                rcv_users_value.Value = model.RCVEDUSERS;
                tb_pubtime.Text = model.PUBDATE.ToString();
                tb_title.Text = model.TITLE;
                tb_titleru.Text = model.TITLERU;
                tb_Files.Text = model.UPLOADFILES;
            }
            else
            {
                tb_pubtime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss");
            }
        }

        /// <summary>
        /// 绑定栏目
        /// </summary>
        private void bindArticleClass()
        {
            mName=ModuleService.GetModuleName(ModuleId);
            ModuleService mService = new ModuleService();
            List<ModuleEntity> list = mService.ModuleList().Where(p => p.MPARENTID == ModuleId).OrderBy(p => p.MORDERBY).ToList();
            if (list.Count > 0)
            {
                tb_classid.DataSource = list;
                tb_classid.DataTextField = "MNAME";
                tb_classid.DataTextFormatString = " └{0}";
                tb_classid.DataValueField = "MODULEID";
                tb_classid.DataBind();               
            }
            tb_classid.Items.Insert(0, new ListItem(mName, ModuleId.ToString()));
            ListItem li = this.tb_classid.Items.FindByValue(ChildId.ToString());
            if (li != null)
                li.Selected = true;
            lb_Nav.Text = mName;

            CreateBT.AddButton("back.gif", "返回", "Index.aspx?ModuleId=" + ModuleId + "&ChildId=" + ChildId, Utility.PopedomType.List, HeadMenu1);
            CreateBT.AddButton("delete.gif", "删除", "ArticlePub.aspx?Id="+Id+"&ModuleId="+ModuleId+"&ChildId="+ChildId+"&Cmd=Delete", Utility.PopedomType.Delete, HeadMenu1);
        }

        //保存
        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                DocArticleEntity model = new DocArticleEntity();
                DocService service = new DocService();
                if (Id != 0)
                {
                    model = service.DocArticleDisp(Id);
                    model.DB_Option_Action = "Update";
                }
                else
                {
                    model.DB_Option_Action = "Insert";
                    model.PASSED = 1;
                    model.ATUIJIAN = 0;
                    model.HITS = 0;
                    model.PUBDATE = DateTime.Now; //tb_pubtime.Text;
                    model.PUBUSER = Utility.Get_UserId;
                }
                model.CLASSID = Convert.ToInt32(tb_classid.SelectedValue);
                model.CONTENTS = tb_content.Text;
                model.CONTENTSRU = tb_contentru.Text;
                model.ARTICLEFROM = tb_from.Text;
                model.KEYS = "";                
                model.TITLE = tb_title.Text;
                model.TITLERU = tb_titleru.Text;
                model.LASTEDITOR = Enterprise.Service.User.UserService.GetUserName(Enterprise.Infrastructure.Utility.Get_UserId);
                model.UPLOADFILES = tb_Files.Text;
                //签收人
                //签收范围格式@1,4,5
                string rcv_users = rcv_users_value.Value;
                rcv_users = rcv_users.TrimEnd(',');
                model.RCVEDUSERS = rcv_users;//签收范围        
                DocService docService = new DocService();
                docService.DocArticleExecute(model);
                if (model.DB_Option_Action == "Insert")
                {
                    Enterprise.Service.Rtx.RtxService.SendRtxMessageServices(model.RCVEDUSERS, " 专题信息 - " + mName, "有新文件需要签收：" + model.TITLE, false);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx?ModuleId=" + ModuleId + "&ChildId=" + ChildId);
        }
    }
}